package com.itheima.blog.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.client.BlogClient;
import com.itheima.blog.admin.domain.po.TCategory;
import com.itheima.blog.admin.dao.TCategoryMapper;
import com.itheima.blog.admin.domain.po.TTag;
import com.itheima.blog.admin.dto.CategoryDto;
import com.itheima.blog.admin.service.ITCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.domain.BlogPageQuery;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.lang.model.element.Name;
import javax.xml.namespace.QName;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 博客分类表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-12
 */
@Service
@RequiredArgsConstructor
public class TCategoryServiceImpl extends ServiceImpl<TCategoryMapper, TCategory> implements ITCategoryService {
    private final TCategoryMapper categoryMapper;
    private final BlogClient blogClient;

    @Override
    public PageResDto queryPage(BlogPageQuery pageQuery) {
        Page<TCategory> page = lambdaQuery().eq(pageQuery.getCategoryName() != null, TCategory::getName, pageQuery.getCategoryName())
                .orderByDesc(TCategory::getSort, TCategory::getClickCount, TCategory::getCreateTime)
                .page(new Page<>(pageQuery.getPage(), pageQuery.getSize()));
        Long total = page.getTotal();
        int total1 = total.intValue();
        Long current = page.getCurrent();
        int currPage = current.intValue();
        Long size = page.getSize();
        int sizes = size.intValue();
        List<TCategory> records = page.getRecords();
        PageResDto pageResDto = new PageResDto<>();
        pageResDto.setCurrentPage(currPage);
        pageResDto.setTotal(total1);
        pageResDto.setSize(sizes);
        pageResDto.setData(records);
        return pageResDto;
    }

    @Override
    public Boolean insertCategory(TCategory category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        boolean save = save(category);
        return save;
    }

    @Override
    public Boolean deleteCategory(List<Integer> ids){
//        List blogIsExist=blogClient.queryBlogByCategoryId(ids);
//        if (blogIsExist !=null){
//            throw new IllegalArgumentException("此分类有博客");
//        }
        boolean b = removeBatchByIds(ids);
        return b;
    }

    @Override
    public CategoryDto queryCategoryList(Integer categoryId) {
        TCategory category = lambdaQuery().eq(TCategory::getId, categoryId).one();
        return CategoryDto.builder().name(category.getName()).content(category.getContent()).build();
    }

    @Override
    public void updateCategory(Integer categoryId, TCategory tCategory) {
        tCategory.setId(categoryId);
        tCategory.setUpdateTime(LocalDateTime.now());
        tCategory.setCreateTime(LocalDateTime.now());
        categoryMapper.updateById(tCategory);
    }

    @Override
    public void categoryStatus(TCategory tCategory) {
        categoryMapper.updateById(tCategory);
    }
}
